package br.edu.ufcg.lsd.wbis.ui.server;

import br.edu.ufcg.lsd.wbis.control.RootUserStatus;
import br.edu.ufcg.lsd.wbis.control.user.UserLevel;
import br.edu.ufcg.lsd.wbis.control.user.UserMatcher;
import br.edu.ufcg.lsd.wbis.control.util.matching.StringMatcher;
import br.edu.ufcg.lsd.wbis.control.util.matching.StringMatcher.StringMatchingType;
import br.edu.ufcg.lsd.wbis.ui.client.beans.RootCreationStatus;
import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo.UserPermission;

/**
 * @author Renato Miceli
 */
public class Util {

	public static UserLevel getEquivalentLevel(UserPermission level) {
		return (level == UserPermission.ADMIN) ? UserLevel.ADMIN : UserLevel.COMMON_USER;
	}

	public static UserPermission getEquivalentLevel(UserLevel level) {
		return level.isAdmin() ? UserPermission.ADMIN : UserPermission.COMMON_USER;
	}

	public static UserMatcher getMatcher(br.edu.ufcg.lsd.wbis.ui.client.beans.UserMatcher matcher) {
		return new UserMatcher().setLogin(matcher.getLogin())
		.setLoginSearchMatcher(getEquivalentStringMatcher(matcher.getLoginType(), matcher.isLoginSensitive()))
		.setEmail(matcher.getEmail())
		.setEmailSearchMatcher(getEquivalentStringMatcher(matcher.getEmailType(), matcher.isEmailSensitive()))
		.setName(matcher.getName())
		.setNameSearchMatcher(getEquivalentStringMatcher(matcher.getNameType(), matcher.isNameSensitive()))
		.setAuthorized(matcher.getAuthorized())
		.setUserLevel(getEquivalentUserLevel(matcher.getLevel()));
	}

	private static UserLevel getEquivalentUserLevel(UserPermission level) {
		if(level == null){
			return null;
		}
		switch (level) {
		case ADMIN:
			return UserLevel.ADMIN;
		default:
			return UserLevel.COMMON_USER;
		}
	}

	private static StringMatcher getEquivalentStringMatcher(br.edu.ufcg.lsd.wbis.ui.client.beans.UserMatcher.StringMatchingType type,
			boolean sensitive) {
		switch(type){
		case DEFAULT:
			return new StringMatcher(sensitive, StringMatchingType.DEFAULT);
		case STARTS_WITH:
			return new StringMatcher(sensitive, StringMatchingType.STARTS_WITH);
		case ENDS_WITH:
			return new StringMatcher(sensitive, StringMatchingType.ENDS_WITH);
		default:
			return new StringMatcher(sensitive, StringMatchingType.CONTAINS);
		}
	}
	public static RootCreationStatus getEquivalentStatus(RootUserStatus status) {
		switch (status) {
		case CREATED:
			return RootCreationStatus.CREATED;
		case CANCELLED:
			return RootCreationStatus.CANCELLED;
		default:
			return RootCreationStatus.NOT_CREATED;
		}
	}

}
